package com.yanzhenjie.andserver;

import com.yanzhenjie.andserver.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;

public class MyCommonEventSubscriber extends CommonEventSubscriber {
    private MainAbilitySlice mainAbilitySlice;

    public MyCommonEventSubscriber(CommonEventSubscribeInfo subscribeInfo, MainAbilitySlice mainAbilitySlice) {
        super(subscribeInfo);
        this.mainAbilitySlice = mainAbilitySlice;
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        Intent intent = commonEventData.getIntent();
        int intParam = intent.getIntParam("andserver", -1);
        if(intParam == -1){
            return;
        }
        switch (intParam){
            case 0:
                mainAbilitySlice.onStopServer();
                break;
            case 1:
                String ip = intent.getStringParam("ip");
                mainAbilitySlice.onStartServer(ip);
                break;
            case 2:
                mainAbilitySlice.onStartServer("服务异常停止，请检查");
                break;
        }

    }
}
